<template>
  <div class="shop">
    <Pane>
      <span slot="title">
        <el-row>
          <el-col :span="3">
            店铺详情
          </el-col>
          <el-col :span="21">
            <el-button @click="handleSave" style="float:right;" type="primary">保存</el-button>
          </el-col>
        </el-row>
      </span>
      <section slot="content">
        <el-form :model="shopData">
          <el-form-item>
            <el-row>
              <el-col :span="2">
                <span>店铺名称</span>
              </el-col>
              <el-col :span="8">
                <el-input v-model="shopData.name" />
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item>
            <el-row>
              <el-col :span="2">
                <span>店铺公告</span>
              </el-col>
              <el-col :span="8">
                <el-input type="textarea" v-model="shopData.bulletin" />
              </el-col>
            </el-row> </el-form-item
          ><el-form-item>
            <el-row>
              <el-col :span="2">
                <span>店铺头像</span>
              </el-col>
              <el-col :span="8">
                <el-avatar shape="square" :size="100" :src="shopData.avatar"></el-avatar>
                <!-- <el-input v-model="shopData.avatar" /> -->
              </el-col>
            </el-row> </el-form-item
          ><el-form-item>
            <el-row>
              <el-col :span="2">
                <span>店铺图片</span>
              </el-col>
              <el-col :span="22">
                <img width="100px" :src="item" v-for="item in shopData.pics" :key="item" alt="" />

                <el-upload
                  action="http://127.0.0.1:5000/shop/upload"
                  list-type="picture-card"
                  :auto-upload="true"
                  :on-success="handleAvatarSuccess"
                  :before-upload="beforeAvatarUpload"
                >
                  <i slot="default" class="el-icon-plus"></i>
                  <div slot="file" slot-scope="{ file }">
                    <img
                      class="el-upload-list__item-thumbnail"
                      :src="file.url"
                      style="width: 100px; height: 100px;"
                      alt=""
                    />
                    <span class="el-upload-list__item-actions" style="width: 100px; height: 100px;">
                      <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleRemove(file)">
                        <i class="el-icon-delete"></i>
                      </span>
                    </span>
                  </div>
                </el-upload>
                <!-- <el-input v-model="shopData.pics" /> -->
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item>
            <el-row>
              <el-col :span="2">
                <span>配送费</span>
              </el-col>
              <el-col :span="8">
                <el-input v-model="shopData.deliveryPrice" />
              </el-col>
            </el-row> </el-form-item
          ><el-form-item>
            <el-row>
              <el-col :span="2">
                <span>配送时间</span>
              </el-col>
              <el-col :span="8">
                <el-input v-model="shopData.deliveryTime" />
              </el-col>
            </el-row> </el-form-item
          ><el-form-item>
            <el-row>
              <el-col :span="2">
                <span>配送描述</span>
              </el-col>
              <el-col :span="8">
                <el-input v-model="shopData.description" />
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item>
            <el-row>
              <el-col :span="2">
                <span>店铺评分</span>
              </el-col>
              <el-col :span="8">
                <el-input v-model="shopData.score" />
              </el-col>
            </el-row> </el-form-item
          ><el-form-item>
            <el-row>
              <el-col :span="2">
                <span>店铺销量</span>
              </el-col>
              <el-col :span="8">
                <el-input v-model="shopData.sellCount" />
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item>
            <el-row>
              <el-col :span="2">
                <span>活动</span>
              </el-col>
              <el-col>
                <el-checkbox-group v-model="shopData.supports">
                  <el-checkbox label="在线支付满25减8"></el-checkbox>
                  <el-checkbox label="VC无限橙果汁全场8折"></el-checkbox>
                  <el-checkbox label="单人精彩套餐"></el-checkbox>
                  <el-checkbox label="特价饮品8折抢购"></el-checkbox>
                  <el-checkbox label="单人特色套餐"></el-checkbox>
                </el-checkbox-group>
                <!-- <el-input v-model="shopData.supports" /> -->
              </el-col>
            </el-row> </el-form-item
          ><el-form-item>
            <el-row>
              <el-col :span="2">
                <span>营业时间</span>
              </el-col>
              <el-col :span="8">
                <el-input v-model="shopData.date" />
              </el-col>
            </el-row>
          </el-form-item>
        </el-form>
      </section>
    </Pane>
  </div>
</template>

<script>
import Pane from '@/components/Pane/Pane'
import { getShopInfo, updateShopInfo } from '@/api/shop.js'
// import { updateAvatar } from '@/api/account.js'
import moment from 'moment'

export default {
  components: {
    Pane
  },
  data() {
    return {
      shopData: {},
      shopImg: [],
      imgBaseUrl: 'http://127.0.0.1:5000/upload/imgs/acc_img/',
      disabled: false,
      newImg: []
    }
  },
  created() {
    this.getShopInfo()
  },
  filters: {
    fiflterCtime: time => {
      return moment(time).format('YYYY-MM-DD HH:MM:SS')
    }
  },
  methods: {
    handleAvatarSuccess(res) {
      const { code, msg, imgUrl } = res
      console.log(res)
      if (code === 0) {
        this.$message.success(msg)
        this.newImg.push(imgUrl)
      }
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/gif'
      const isLt2M = file.size / 1024 / 1024 < 4

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG,png,gif 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 4MB!')
      }
      return isJPG && isLt2M
    },
    // 保存
    async handleSave() {
      // updateShopInfo()
      const resultImg = []
      this.shopData.pics.forEach(v => {
        const imgUrl = v.split('/')
        v = imgUrl[imgUrl.length - 1]
        resultImg.push(v)
      })
      if (this.newImg.length) {
        resultImg.push(...this.newImg)
      }
      this.shopData.pics = resultImg
      console.log(this.shopData)
      const data = await updateShopInfo({
        id: this.shopData.id,
        name: this.shopData.name,
        bulletin: this.shopData.bulletin,
        avatar: this.shopData.avatar,
        deliveryPrice: this.shopData.deliveryPrice,
        deliveryTime: this.shopData.deliveryTime,
        description: this.shopData.description,
        score: this.shopData.score,
        sellCount: this.shopData.sellCount,
        supports: JSON.stringify(this.shopData.supports),
        pics: JSON.stringify(this.shopData.pics),
        date: JSON.stringify(this.shopData.date)
      })
      // supports: JSON.stringify(this.shopData.supports),
      //   pics: JSON.stringify(this.shopData.pics),
      //   date: JSON.stringify(this.shopData.date)
      console.log(data)
      if (data.code === 0) {
        // location.reload()
        // this.getShopInfo()
        location.reload()
      }
    },
    // handleSave() {},
    // 获取信息
    async getShopInfo() {
      const { data } = await getShopInfo()
      console.log(data)
      this.shopData = data
      // let str = 'http://127.0.0.1:5000/upload/shop/'
      // str += this.shopData.avatar
      // this.shopData.avatar = str
      // console.log(this.shopData.avatar)
      // console.log("-->>>>>>>>>"+this.shop)
      for (let index = 0; index < this.shopData.pics.length; index++) {
        // const element = array[index];
        this.shopImg[index] = 'http://127.0.0.1:5000/upload/shop/' + this.shopData.pics[index]
      }
      this.shopData.pics = this.shopImg
      // console.log(this.shopData.pics)
    },
    handleRemove(file) {
      console.log(file)
      file.url = ''
      // this.handleRemove(file)
    }
    // handlePictureCardPreview(file) {
    //   console.log(file)
    // }
  }
}
</script>

<style scoped>
.shop >>> .el-textarea__inner {
  height: 100px;
}
.shop >>> .el-upload--picture-card {
  position: relative;
  width: 100px;
  height: 100px;
}
.shop >>> .el-icon-plus {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.shop >>> .el-upload-list__item {
  width: 100px;
  height: 100px;
}
</style>
